package com.shouxin.attendance.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.shouxin.attendance.R;
import com.shouxin.attendance.adapter.HistoryAdapter;
import com.shouxin.attendance.database.DBHelper;
import com.shouxin.attendance.database.dao.SwipeCardHistoryDao;
import com.shouxin.attendance.database.model.SwipeCardHistory;

import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>刷卡历史纪录界面</p>
 * Created by Jiang on 15-10-29.
 */
public class UploadActivity extends BaseActivity {

    private RecyclerView mRecyclerView;

    private List<SwipeCardHistory> mData = new ArrayList<>();

    private HistoryAdapter mAdapter;

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_history);
    }

    @Override
    protected void initUIViews() {
        mRecyclerView = (RecyclerView) findViewById(R.id.history_recyclerView);
    }

    @Override
    protected void initData() {
        mData.clear();
        loadHistoryByPage();
        mAdapter = new HistoryAdapter(mData, getLayoutInflater());
    }

    private void loadHistoryByPage() {
        SwipeCardHistoryDao historyDao = DBHelper.getInstance().getSwipeCardHistoryDao();
        List<SwipeCardHistory> historyList = historyDao.queryBuilder()
                .where(new WhereCondition.StringCondition("_ID IN (SELECT p.SEND_TIME FROM t_push_data p)"))
                .orderDesc(SwipeCardHistoryDao.Properties.TimeStamp).list();
        mData.addAll(historyList);
    }

    @Override
    protected void bindEvents() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(true);
        mRecyclerView.setItemViewCacheSize(10);
        mRecyclerView.setAdapter(mAdapter);
    }
}
